package com.example.feifei.net;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by feifei on 2018/6/5.
 */

public class RetrofitClient {
    private static RetrofitClient mInstance;

    private OkHttpClient okHttpClient;

    private ApiService apiService;

    //构造函数
    private RetrofitClient(){
        okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10,TimeUnit.SECONDS)
                .writeTimeout(10,TimeUnit.SECONDS)
                .build();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(UrlManager.BASE_NewURL)
                .client(okHttpClient)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        apiService = retrofit.create(ApiService.class);
    }

    //单例对象
    public static RetrofitClient getInstance(){
        if(null == mInstance){
            mInstance = new RetrofitClient();
        }
        return mInstance;
    }

    public ApiService getApiService(){
        if(apiService != null){
            return apiService;
        }
        return null;
    }

}
